﻿Imports System.Text
Imports System.IO.Ports

Public Class add

    Dim memoryImage As Bitmap
    Dim work_id_check_return As Boolean
    Dim card_id_check_return As Boolean

    '初始化
    Private Sub add_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: 这行代码将数据加载到表“TestDataSet.zgb”中。您可以根据需要移动或删除它。
        Me.ZgbTableAdapter1.Fill_all_data(Me.TestDataSet2.zgb)
    End Sub

    '取消
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
        mainview.Enabled = True
    End Sub

    '选择照片
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        OpenFileDialog1.ShowDialog()
        PictureBox1.ImageLocation = OpenFileDialog1.FileName.ToString
    End Sub

    '截屏函数
    Private Sub CaptureScreen()
        Dim myGraphics As Graphics = yuangong_print.CreateGraphics()
        Dim s As Size = yuangong_print.Size
        memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
        Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
        memoryGraphics.CopyFromScreen(yuangong_print.Location.X, yuangong_print.Location.Y, 0, 0, s)
    End Sub

    '设置打印
    Private Sub printDocument1_PrintPage(ByVal sender As System.Object, _
       ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
       PrintDocument1.PrintPage
        e.Graphics.DrawImage(memoryImage, 0, 0)
    End Sub

    Private Sub DataCheck()
        '检查工号
        If Me.TextBox1.Text > 999 Or Me.TextBox1.Text < 0 Then
            MsgBox("工号输入错误，请重新输入")
        End If

        '检查年龄
        '检查身份证号码
        '检查卡号

    End Sub

    '增加条目
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        '校验数据有效性
        DataCheck()
        '写入新条目
        Me.ZgbTableAdapter1.Insert(TextBox1.Text, TextBox2.Text, TextBox3.Text, Me.ComboBox1.SelectedIndex, TextBox4.Text, TextBox5.Text, PictureBox1.ImageLocation)
        '打印员工卡
        yuangong_print.Show()
        yuangong_print.PictureBox1.ImageLocation = Me.PictureBox1.ImageLocation
        yuangong_print.Label5.Text = Me.TextBox2.Text
        yuangong_print.Label6.Text = Me.TextBox1.Text
        MsgBox("增加成功!        正在打印，请稍后")
        CaptureScreen()
        PrintDocument1.Print()
        yuangong_print.Close()
        '清屏，设置焦点
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        PictureBox1.ImageLocation = ""
        Me.ZgbTableAdapter1.Fill_all_data(Me.TestDataSet2.zgb)
        Me.TextBox1.Focus()
    End Sub

    '卡号唯一性校验  回车键激活
    Private Sub TextBox3_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox3.KeyDown
        If (e.KeyValue = 13) Then
            Dim num As Integer

            card_id_check_return = False

            Me.DataGridView1.CurrentCell = Me.DataGridView1(3, 0)

            For num = 1 To Me.DataGridView1.Rows.Count
                Me.DataGridView1.CurrentCell = Me.DataGridView1(3, num - 1)
                If Me.TextBox3.Text = Me.DataGridView1.CurrentCell.Value Then
                    card_id_check_return = True
                End If
            Next
            If card_id_check_return Then
                MsgBox("卡号不唯一，请重新输入！")
                Me.TextBox3.Text = ""
                Me.TextBox3.Focus()
            End If
        End If
    End Sub

    '工号唯一性校验
    Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        Dim num As Integer

        work_id_check_return = False

        Me.DataGridView1.CurrentCell = Me.DataGridView1(1, 0)

        For num = 1 To Me.DataGridView1.Rows.Count
            Me.DataGridView1.CurrentCell = Me.DataGridView1(1, num - 1)
            If Me.TextBox1.Text = Me.DataGridView1.CurrentCell.Value Then
                work_id_check_return = True
            End If
        Next
        If work_id_check_return Then
            MsgBox("工号不唯一，请重新输入！")
            Me.TextBox1.Text = ""
            Me.TextBox1.Focus()
        End If

    End Sub


End Class